package com.zzxz.practicaltrainingsys.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzxz.practicaltrainingsys.entity.Dailynews;
import com.zzxz.practicaltrainingsys.mapper.DailynewsMapper;
import com.zzxz.practicaltrainingsys.service.DailynewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class DailynewsServiceImpl implements DailynewsService {
        @Autowired
        private DailynewsMapper dailynewsMapper;
    //用来联合学生表的学号以及项目表的项目编号来查该项目该学生的日报
    @Override
    public List<Dailynews> getDailynewsByStudentIdAndProjectId(Integer studentId, Integer projectId) {
        if (studentId == null || projectId == null) {
            throw new IllegalArgumentException("学生ID和项目ID不能为空");
        }
        List<Dailynews> dailynewsList = dailynewsMapper.getDailynewsByStudentIdAndProjectId(studentId, projectId);
        return dailynewsList;
    }

    @Override
    public Dailynews getDailynewsById(Integer dailynewsId) {
        if (dailynewsId == null) {
            throw new IllegalArgumentException("日报 ID 不能为空");
        }
        return dailynewsMapper.getDailynewsById(dailynewsId);
    }

    @Override
    public int insertDailynews(Dailynews dailynews) {
        return dailynewsMapper.insertDailynews(dailynews);
    }

    @Override
    public int updateDailynews(Dailynews dailynews) {
        return dailynewsMapper.updateDailynews(dailynews);
    }

    @Override
    public List<Map<String, Object>> getAllDailyReports() {
        return dailynewsMapper.getAllDailyReports();
    }

    @Override
    public PageInfo<Map<String, Object>> getDailyNewsByPage(Integer pageNum, Integer pageSize) {
        // 1. 参数校验（避免 null 或非法值）
        if (pageNum == null || pageNum <= 0) pageNum = 1;
        if (pageSize == null || pageSize <= 0) pageSize = 10;

        // 2. 启动 PageHelper 分页（必须紧跟查询）
        PageHelper.startPage(pageNum, pageSize);

        // 3. 执行查询（PageHelper 会自动拦截并添加 LIMIT 语法）
        List<Map<String, Object>> dailyNewsList = dailynewsMapper.getAllDailyNews();

        // 4. 用 PageInfo 包装结果（包含总记录数、分页信息）
        return new PageInfo<>(dailyNewsList);
    }



    @Override
    public List<Map<String, Object>> getAllDailyNews() {
        return dailynewsMapper.getAllDailyNews();
    }

    @Override
    public List<Map<String, Object>> getDailyNewsByProjectId(Integer projectId) {
        ;if (projectId == null || projectId <= 0) {
            throw new IllegalArgumentException("项目ID无效");
        }
        return dailynewsMapper.getDailyNewsByProjectId(projectId);
    }

}

